from  selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
import time
class Base():
    def __init__(self,driver):
        self.driver=driver

    def open_url(self,url):
        self.driver.get(url)

    #时间
    def time_pic(self, name):
        t = time.strftime("%Y%m%d%H%M%S", time.localtime())
        self.driver.save_screenshot(t + name)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.time_pic('error.png')
            print(e)

    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)


    def click(self,loc):
        self.find_element(loc).click()

    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

        # 下拉菜单封装
    def select_value(self, loc, value):
        city = Select(self.find_element(loc))
        city.select_by_value(value)

    def select_index(self, loc, index):
        city = Select(self.find_element(loc))
        city.select_by_index(index)

    def select_text(self, loc, visible):
        city = Select(self.find_element(loc))
        city.select_by_visible_text(visible)

        # 滚动事件封装
        # 滚动到顶部

    def scrollTo_top(self):
        self.driver.execute_script('window.scrollTo(0,0)')

        # 滚动到底部
    def scrollTo_bush(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollToHeight)')

        # 向下滚动任意元素
    def scrollTo_div(self, s):
        self.driver.execute_script(f'window.scrollTo(0,{s})')

        # 弹窗
    def alert_accept(self):
        alert = self.driver.switch_to.alert
        alert.accecp()
        alert.dismiss()
        alert.send_keys()

    def alert_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()

    def alert_send_keys_accept(self):
        alert = self.driver.switch_to.alert
        alert.send_keys()
        alert.accecp()

    def alert_send_keys_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.send_keys()
        alert.dismiss()

        # 进入框架

    def switch_to_frame(self, loc):
        self.driver.switch_to.frame(self.find_element(loc))

        # 退出框架

    def default_content(self):
        self.driver.switch_to.default_content()

        # 封装鼠标事件
        # 双击

    def double_click(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).double_click(ele).perform()

        # 三击

    def three_click(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).double_click(ele).perform()

    def move_to(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).move_to_element(ele).perform()

    def drag_and_drop(self, loc, x, y):
        ele = self.find_element(loc)
        ActionChains(self.driver).drag_and_drop_by_offset(ele, x, y).perform()

    def click_hold(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).click_and_hold(ele).perform()

    def release(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).release(ele).perform()

    def context_click(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).context_click(ele).perform()

    #刷新
    def refresh(self):
        self.driver.refresh()